package com.solution.admin.company.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.company.entity.CompanyInfoEntity;
import com.solution.admin.company.service.CompanyInfoService;
import com.solution.admin.settings.entity.KeyMapEntity;
import com.solution.admin.settings.service.KeyMapService;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.string.StringUtil;

/**
 * 公司基本信息web控制层
 * @author lufer
 * @date 2017-10-23 16:20:45
 */
@RequestMapping("/companyInfo")
@Controller
public class CompanyInfoController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CompanyInfoController.class);

	@Autowired
	private CompanyInfoService companyInfoService;
	
	@Autowired
	private KeyMapService keyMapService;
	
	
	/**
	 * 跳转新增/修改页面
	 * @return string 跳转页面路径
	 * @author lufer
 	 * @date 2017-10-23 16:20:45
	 */
	@RequestMapping("/toAddOrUpdateCompanyInfo")
	public String toAddOrUpdateCompanyInfo(HttpServletRequest request, String tabIndex) {
		String id = request.getParameter("id");
		//如果是修改操作
		if(StringUtil.isNotEmpty(id)){
			//获取公司信息,并传递到修改页面进行回显
			CompanyInfoEntity companyInfoEntity = companyInfoService.getCompanyInfoById(Long.valueOf(id));
			request.setAttribute("companyInfo", companyInfoEntity);
		}
		if (StringUtil.isEmpty(tabIndex)) {
			request.setAttribute("tabIndex", "1");
        } else {
        	request.setAttribute("tabIndex", tabIndex);
        }
		return "company/companyInfo_form";
	}
	
	/**
	 * 查询公司行业配置信息
	 * 作者：lufer
	 * 时间：2017年10月27日 下午5:42:23
	 * @param request
	 * @return
	 */
	@RequestMapping("/getIndustryList")
    @ResponseBody
    public List<KeyMapEntity> getProductUnitList(HttpServletRequest request) {
		//查询公司行业配置信息，公司行业类型为“companyIndustry”
		List<KeyMapEntity> keyMapList = keyMapService.listKeyMap("companyIndustry");
		return keyMapList;
	}


	/**
	 * 新增或保存公司基本信息
	 * @param request 请求对象
	 * @param companyInfoEntity 公司基本信息实体
	 * @return
	 * @author lufer
 	 * @date 2017-10-23 16:20:45
	 */
	@RequestMapping("/saveOrUpdateCompanyInfo")
	@ResponseBody
	public Map<String, Object> saveCompanyInfo(HttpServletRequest request, CompanyInfoEntity companyInfoEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				companyInfoEntity.setModifier(getUserCode());
				result = companyInfoService.updateCompanyInfo(companyInfoEntity);
			}else{
				companyInfoEntity.setCreater(getUserCode());
				result = (int)companyInfoService.saveCompanyInfo(companyInfoEntity);
			}
			resultMap.put("code", result);
			System.out.print("**************"+result);
		} catch (Exception e) {
			handleException("添加公司基本信息失败", logger, e);
		}
		return resultMap;
	}
	
}
